package info.thereisonlywe.onenesspathfinder.ui;

import info.thereisonlywe.core.toolkit.CharacterToolkit;
import info.thereisonlywe.core.toolkit.ThreadToolkit;
import info.thereisonlywe.onenesspathfinder.controllers.RecitationController;
import info.thereisonlywe.quran.Quran;
import info.thereisonlywe.quran.QuranicVerse;

import java.awt.KeyEventDispatcher;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;

public class KeyDispatcher implements KeyEventDispatcher {

	private String typedCtrl = "";
	private String typedAlt = "";

	private static long lastRequestTime = 0;

	@Override
	public boolean dispatchKeyEvent(final KeyEvent e)
	{
		if (UIDelegate.isDialogOpen()
				|| RecitationController.getImam().isDialogOpen()
				|| MainFrame.view_Windows.getSearchPanel().hasFocus()
				|| MainFrame.view_Windows.getScratchPanel().hasFocus()) { return false; }
		if (e.getID() == KeyEvent.KEY_PRESSED)
		{
			if (CharacterToolkit.isDigit(e.getKeyCode()))
			{
				if (e.isControlDown())
				{
					typedCtrl += (char) e.getKeyCode();
				}
				if (e.isAltDown())
				{
					typedAlt += (char) e.getKeyCode();
				}
			}
			else
			{
				if (e.getKeyCode() == KeyEvent.VK_RIGHT)
				{
					if (e.isControlDown())
					{
						UIDelegate.seekToLastVerseInSection();
					}
					else if (e.isAltDown())
					{
						UIDelegate.seekToNextSection();
					}
					else
					{
						UIDelegate.seekToNextVerse();
					}
				}
				else if (e.getKeyCode() == KeyEvent.VK_LEFT)
				{
					if (e.isControlDown())
					{
						UIDelegate.seekToFirstVerseInSection();
					}
					else if (e.isAltDown())
					{
						UIDelegate.seekToPreviousSection();
					}
					else
					{
						UIDelegate.seekToPreviousVerse();
					}
				}

				// else if (e.getKeyCode() == KeyEvent.VK_TAB)
				// {
				// if (!e.isAltDown())
				// {
				// MainFrame.view_Windows.unmountAll();
				// }
				//
				// }
			}
		}
		else if (e.getID() == KeyEvent.KEY_RELEASED)
		{
			if (e.getKeyCode() == KeyEvent.VK_CONTROL)
			{
				if (typedCtrl.isEmpty())
				{

				}
				else
				{
					if (typedCtrl.length() <= 3)
					{
						final int typedI = Integer.parseInt(typedCtrl);
						final QuranicVerse v = Quran
								.getVerse(UIDelegate.getDisplayedVerse()
										.getSectionNumber(), typedI);
						if (v != null)
						{
							UIDelegate.seekToVerse(v);
							UIDelegate.refreshVerse();
						}
						else
						{
							Toolkit.getDefaultToolkit().beep();
						}
					}
					typedCtrl = "";
				}

			}
			else if (e.getKeyCode() == KeyEvent.VK_ALT)
			{
				if (typedAlt.isEmpty())
				{

				}
				else
				{
					if (typedAlt.length() <= 3)
					{
						final int typedI = Integer.parseInt(typedAlt);
						final QuranicVerse v = Quran.getVerse(typedI, 1);
						if (v != null)
						{
							UIDelegate.seekToVerse(v);
							UIDelegate.refreshVerse();
						}
						else
						{
							Toolkit.getDefaultToolkit().beep();
						}
					}
					typedAlt = "";
				}
			}
			else if (e.getKeyCode() == KeyEvent.VK_RIGHT
					|| e.getKeyCode() == KeyEvent.VK_LEFT)
			{
				if (e.isShiftDown())
				{
				}
				else
				{
					final Thread runRequest = new Thread()
					{
						@Override
						public void run()
						{
							lastRequestTime = System.currentTimeMillis();
							final long firstValue = System.currentTimeMillis()
									- lastRequestTime;
							long t = firstValue;
							while (t < 50)
							{
								ThreadToolkit.sleep(10);
								t = System.currentTimeMillis()
										- lastRequestTime;
								if (t < firstValue) { return; }
							}

							if (e.isAltDown())
							{
								UIDelegate.refreshSectionAndResetVerse();
							}
							else
							{
								UIDelegate.refreshVerse();
							}
						}
					};

					new Thread(runRequest).start();
				}
			}

			else if (e.getKeyCode() == KeyEvent.VK_F)
			{
				if (!MainFrame.view_Windows.isSearchContainerMounted())
				{
					MainFrame.view_Windows.mountSearchContainer();
				}
				MainFrame.view_Windows.getSearchPanel().requestFocus();
			}

			else if (e.getKeyCode() == KeyEvent.VK_F11)
			{
				UIDelegate.toggleFullScreen();
			}

			else if (e.getKeyCode() == KeyEvent.VK_ENTER)
			{
				if (RecitationController.getImam().isPaused())
				{
					RecitationController.getImam().revive();
				}
				else
				{
					if (e.isAltDown())
					{
						RecitationController.reciteSection();
					}
					else if (e.isControlDown())
					{
						if (!RecitationController.getImam().isAlive()
								|| !UIDelegate.isBrowserLocked())
						{

							RecitationDialog.showDialog();
						}
					}
					else
					{
						RecitationController.reciteVerse();
					}
				}
			}
			else if (e.getKeyCode() == KeyEvent.VK_SPACE)
			{
				RecitationController.getImam().terminate();
				if (!RecitationController.getImam().isAlive())
				{
					UIDelegate.disEngageBrowserLock();
				}
			}
			else if (e.getKeyCode() == KeyEvent.VK_D)
			{
				if (e.isControlDown())
				{
					if (UIDelegate.BookmarkPaneHook.getBookmarkList().contains(
							UIDelegate.getDisplayedVerse()))
					{
						UIDelegate.BookmarkPaneHook
								.removeDisplayedVerseFromBookmarks();
					}
					else
					{
						UIDelegate.BookmarkPaneHook.addBookmarkWindow();
					}
				}
			}
			else if (e.getKeyCode() == KeyEvent.VK_UP)
			{
				if (e.isControlDown())
				{
					UIDelegate.increaseVolume();
				}
			}
			else if (e.getKeyCode() == KeyEvent.VK_DOWN)
			{
				if (e.isControlDown())
				{
					UIDelegate.decreaseVolume();
				}
			}
		}
		return false;
	}
}
